<?php

namespace app\common\service\queue;

use app\common\service\ApiService;
use app\common\service\ConfigService;
use think\facade\Log;
use think\facade\Queue;

class QueueService extends ApiService
{

    public function addWordSplitJob(array $wordData = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\WordRunner';
        $jobData = [
            'type' => 'word_split',
            'word_data' => $wordData,
            'created_at' => time()
        ];
        $jobQueue = 'word_split';
        try {
            if ($delay > 0) {
                Queue::later($delay, $jobHandlerClass, $jobData, $jobQueue);
            } else {
                Queue::push($jobHandlerClass, $jobData, $jobQueue);
            }
            return true;
        } catch (\Exception $e) {
            \think\facade\Log::error('添加队列任务失败: ' . $e->getMessage());
            return false;
        }
    }

    public function addWordVocalJob(array $wordData = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\WordVocalRunner';
        $jobData = [
            'type' => 'word_vocal',
            'word_data' => $wordData,
            'created_at' => time()
        ];
        $jobQueue = 'word_vocal';
        try {
            if ($delay > 0) {
                Queue::later($delay, $jobHandlerClass, $jobData, $jobQueue);
            } else {
                Queue::push($jobHandlerClass, $jobData, $jobQueue);
            }
            return true;
        } catch (\Exception $e) {
            Log::error('添加队列任务失败: ' . $e->getMessage());
            return false;
        }
    }

    /*
     * 生成文章题目
     * */
    public function getAiArticleQuestionJob(array $data = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\AiArticleQuestionRunner';
        $jobData = $data;
        $jobQueue = 'ai_article_question';
        try {
            Queue::push($jobHandlerClass, $jobData, $jobQueue);
            return true;
        } catch (\Exception $e) {
            \think\facade\Log::error('添加队列任务失败ai_article_question: ' . $e->getMessage());
            return false;
        }
    }

    /*
     * 通过文章内容生成短句
     * */
    public function getArticleSentenceByContentJob(array $data = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\AiArticleSentenceRunner';
        $jobData = $data;
        $jobQueue = 'ai_article_sentence';
        try {
            //            if ($delay > 0) {
            //                Queue::later($delay, $jobHandlerClass, $jobData, $jobQueue);
            //            } else {
            //                Queue::push($jobHandlerClass, $jobData, $jobQueue);
            //            }
            Queue::push($jobHandlerClass, $jobData, $jobQueue);
            return true;
        } catch (\Exception $e) {
            \think\facade\Log::error('添加队列任务失败ai_article_sentence: ' . $e->getMessage());
            return false;
        }
    }

    /*
     * 通过文章内容生成文章核心词汇
     * */
    public function getArticleWordByContent(array $data = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\AiArticleWordRunner';
        $jobData = $data;
        $jobQueue = 'ai_article_word';
        try {
            //            if ($delay > 0) {
            //                Queue::later($delay, $jobHandlerClass, $jobData, $jobQueue);
            //            } else {
            //                Queue::push($jobHandlerClass, $jobData, $jobQueue);
            //            }
            Queue::push($jobHandlerClass, $jobData, $jobQueue);
            return true;
        } catch (\Exception $e) {
            \think\facade\Log::error('添加队列任务失败ai_article_word: ' . $e->getMessage());
            return false;
        }
    }

    /*
     * 通过文章内容生成文章核心词汇
     * */
    public function getArticleParagraphByContent(array $data = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\AiArticleParagraphRunner';
        $jobData = $data;
        $jobQueue = 'ai_article_tense';
        try {
            //            if ($delay > 0) {
            //                Queue::later($delay, $jobHandlerClass, $jobData, $jobQueue);
            //            } else {
            //                Queue::push($jobHandlerClass, $jobData, $jobQueue);
            //            }
            Queue::push($jobHandlerClass, $jobData, $jobQueue);
            return true;
        } catch (\Exception $e) {
            \think\facade\Log::error('添加队列任务失败ai_article_tense: ' . $e->getMessage());
            return false;
        }
    }

    /*
     * 通过文章内容生成文章句子结构
     * */
    public function getArticleSentenceStructureByContent(array $data = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\AiArticleSentenceStructureRunner';
        $jobData = $data;
        $jobQueue = 'ai_article_sentence_structure';
        try {
            //            if ($delay > 0) {
            //                Queue::later($delay, $jobHandlerClass, $jobData, $jobQueue);
            //            } else {
            //                Queue::push($jobHandlerClass, $jobData, $jobQueue);
            //            }
            Queue::push($jobHandlerClass, $jobData, $jobQueue);
            return true;
        } catch (\Exception $e) {
            \think\facade\Log::error('添加队列任务失败ai_article_sentence_structure: ' . $e->getMessage());
            return false;
        }
    }

    // 结束课程
    public function addEndSkJob(array $data = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\EndSk';
        $jobData = $data;
        $jobQueue = 'end_sk';
        $ConfigService = new ConfigService();
        $delayEndMinutes = $ConfigService->getSysConfigValue('course_delay_end_minutes') ?? 0;
        $delay = $delay + ($delayEndMinutes * 60);
        try {
            if ($delay > 0) {
                Queue::later($delay, $jobHandlerClass, $jobData, $jobQueue);
            } else {
                Queue::push($jobHandlerClass, $jobData, $jobQueue);
            }
            return true;
        } catch (\Exception $e) {
            \think\facade\Log::error('添加队列任务失败: ' . $e->getMessage());
            return false;
        }
    }

    public function addSayWordSplitJob(array $wordData = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\SayWordRunner';
        $jobData = [
            'type' => 'word_split',
            'word_data' => $wordData,
            'created_at' => time()
        ];
        $jobQueue = 'say_word_split';
        try {
            if ($delay > 0) {
                Queue::later($delay, $jobHandlerClass, $jobData, $jobQueue);
            } else {
                Queue::push($jobHandlerClass, $jobData, $jobQueue);
            }
            return true;
        } catch (\Exception $e) {
            \think\facade\Log::error('添加队列任务失败: ' . $e->getMessage());
            return false;
        }
    }

    public function addSayWordVocalJob(array $wordData = [], int $delay = 0)
    {
        $jobHandlerClass = 'app\common\job\SayWordVocalRunner';
        $jobData = [
            'type' => 'word_vocal',
            'word_data' => $wordData,
            'created_at' => time()
        ];
        $jobQueue = 'say_word_vocal';
        try {
            if ($delay > 0) {
                Queue::later($delay, $jobHandlerClass, $jobData, $jobQueue);
            } else {
                Queue::push($jobHandlerClass, $jobData, $jobQueue);
            }
            return true;
        } catch (\Exception $e) {
            Log::error('添加队列任务失败: ' . $e->getMessage());
            return false;
        }
    }

}